package com.jumper.mydemo.statics;

import java.io.IOException;

import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.core.type.filter.TypeFilter;

import com.jumper.mydemo.my.MyAnnotation;

public class TypeFilterDemo {
    public static void run() throws IOException {
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		SimpleMetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
		TypeFilter annotationFilter = new AnnotationTypeFilter(MyAnnotation.class);

		Resource[] resources = resolver.getResources("classpath*:com/jumper/mydemo/**/*.class");

		for(Resource resource : resources) {
			MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
			boolean match = annotationFilter.match(metadataReader, metadataReaderFactory);
			System.out.printf("扫描到文件:%-20s || 筛选器是否匹配:%s%n", resource.getFile().getName(), match);
		}
    }   
}
